package com.unit.test;


import com.unit.test.model.User;
import com.unit.test.service.ServiceA;
import com.unit.test.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

@Slf4j
public class MockitoTest_MockBean extends BaseTest {

    //需要mock注入的服务
    @Autowired
    private ServiceA serviceA;

    //需要mock掉的服务
    @MockBean
    private UserService userService;

    @Test
    public void test() {
        User user = new User();
        user.setId(111L);
        user.setName("李四");
        user.setAge(99);
        //mock掉ServiceC的getUser()方法，返回指定的user对象
        when(userService.getById(anyInt())).thenReturn(user);

        //调用需要测试的方法，结果将是mock结果
        User resultUser = serviceA.getUserVo(123);
        log.info(resultUser.toString());
        Assert.assertEquals(resultUser.getAge(), user.getAge());
    }
}



